﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Kickstarter.Models;

namespace WebTV_Kickstarter
{
    [MetadataType(typeof(Donation_Validator))]
    public partial class Donation
    {
        public static void AddDonation(Donation donation, EFDataModelContainer tempContainer)
        {
            EFDataModelContainer db = tempContainer;
            db.DonationSet.Add(donation);
            db.SaveChanges();
        }


        public static void AddDonationFull(Donation donation, int? id)
        {
            Project p = Project.FindProject(donation.ProjectId);

            Gebruiker sessionGebruiker = (Gebruiker)HttpContext.Current.Session["Gebruiker"];
            Gebruiker g = Gebruiker.FindGebruikerWithDbContainer(sessionGebruiker.Id, Project.GetTempDbContainer());
            g.TotalDonated += donation.Amount;
            Gebruiker.EditGebruikerForDonation(g);

            donation.User = g;
            p.TotalAmount += donation.Amount;

            donation.Project = p;

            Project.EditProjectFromDonation(p);

            //Voeg donatie toe
            AddDonation(donation, Project.GetTempDbContainer());

            Project.DisposeTempDbContainer();
        }


        public static void AddDonationAnoniemFull(AnoDonation donation, int? id)
        {
            Project p = Project.FindProject(donation.ProjectId);

            p.TotalAmount += donation.Amount;

            donation.Project = p;

            Project.EditProjectFromDonation(p);

            //Voeg donatie toe
            AnoDonation.AddAnoDonation(donation, Project.GetTempDbContainer());

            Project.DisposeTempDbContainer();
        }

        internal static Donation[] getDonationsFromUser(int id)
        {
            EFDataModelContainer db = new EFDataModelContainer();
            Gebruiker g = Gebruiker.FindGebruiker(id);
            ICollection<Donation> donation = g.Donation;
            Donation[] donationArray =
            (
                from d in donation
                where d.User == g 
                select d
            ).ToArray();

            return donationArray;
        }
    }

    public partial class AnoDonation
    {
        public static void AddAnoDonation(AnoDonation donation, EFDataModelContainer tempContainer)
        {
            EFDataModelContainer db = tempContainer;
            db.AnoDonationSet.Add(donation);
            db.SaveChanges();
        }
        public bool Agree { get; set; }
    }
}